/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import DTO.DTOManufacture;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Pham
 */
public class DALManu {
    ConnectionDB conn;
    
    public DALManu()
    {
        conn = new ConnectionDB();
    }
    
    //method get all manufacture
    public ArrayList<DTOManufacture> getAllManu()
    {
        try{
            ArrayList<DTOManufacture> listManu = new ArrayList<>();
            String query = "select * from manufacture";
            ResultSet rs = conn.executeQuery(query);
            while(rs.next())
            {
                listManu.add(new DTOManufacture(rs));
            }
            return listManu;
        }catch(Exception ex)
        {
            System.err.print(ex.getMessage());
            return null;
            
        }
    }
    
    //method get manufacture by ID
    public DTOManufacture getManuByID(int ID)
    {
        try
        {
            DTOManufacture manu = new DTOManufacture();
            String query = "select * from manufacture where manufacture_id = " + ID;
            ResultSet rs = conn.executeQuery(query);
            while(rs.next())
            {
                manu = new DTOManufacture(rs);
            }
            return manu;
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
            return null;
        }
    }
    
    //method insert new manufacture
    public boolean insertNewManu(DTOManufacture dtoManu)
    {
        try{
            String sqlInsert = "insert into manufacture(manufacture_name) values('" + dtoManu.getManuName() + "')";
            if(conn.exeUpdate(sqlInsert))
                return true;
            return false;
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
            return false;
        }
    }
    
    //method update manufacture
    public boolean updateManu(DTOManufacture dtoManu)
    {
        try{
            String sqlUpdate = "update manufacture set manufacture_name = '"+dtoManu.getManuName()+"' where manufacture_id = " + dtoManu.getManuID();
            if(conn.exeUpdate(sqlUpdate))
                return true;
            return false;
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
            return false;
        }
    }
    
    //method delete a manufacture
    public boolean deleteManu(int idManu)
    {
        try
        {
            String sqlDel = "delete from manufacture where manufacture_id = " + idManu;
            if(conn.exeUpdate(sqlDel))
                return true;
            return false;
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
            return false;
        }
    }
}
